package com.jjyd.crlp.util.news;

import com.jjyd.crlp.domain.entity.News;
import com.jjyd.crlp.domain.repository.NewsRepo;
import com.jjyd.crlp.util.check.CheckUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * 获取新闻工具类，新线程上跑起
 */
public class GetNewsUtil implements Runnable{
    /**
     * 新闻dao
     */
    private NewsRepo newsRepo;

    /**
     * baseUrl
     */
    private static final String URL = "http://ru.tingroom.com/wap/index.php?moduleid=28&itemid=";
    /**
     * 最多存储的条数
     */
    private static final Long UPDATE_QUANTITY = 10L;
    /**
     * 执行线程的时间间隔
     */
    private static final Long TIME_INTERVAL = 24 * 60 * 60 * 1000L;


    public GetNewsUtil(NewsRepo newsRepo) {
        this.newsRepo = newsRepo;
    }

    /**
     * 新线程，用于爬虫抓去网站任务
     */
    @Override
    public void run(){
        while (true){
            getNews();
            try {
                Thread.sleep(TIME_INTERVAL);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }


    private Long getNews() {
        News news = newsRepo.findTopByRemoveFalseOrderByItemIdDesc();
        Long itemId;
        if (news == null) {
            itemId = 30095L;
        } else {
            itemId = news.getItemId();
        }
        String url = URL + itemId;
        System.out.println(url);
        Long webMaxItemId = maximalNewsItemId(itemId);
        //存储数量
        Long quantity = saveNews(itemId, webMaxItemId);

        return quantity;
    }

    /**
     * 判断有无更多的新闻
     * @param itemId 目前数据库最大的itemId
     * @return
     */
    private Long maximalNewsItemId(Long itemId) {
        CheckUtil.check("itemId不能为空", itemId != null);
        long webItemId = itemId;
        while (true) {
            String url = URL + webItemId;
            //通过延迟5000毫秒然后再去请求可解决js异步加载获取不到数据的问题
            Document doc= null;
            try {
                doc = Jsoup.connect(url).timeout(5000).get();
            } catch (IOException e) {
                continue;
            }
            //获取title，没有的话则size为0
            Elements elements = doc.getElementsByClass("title");
            if (elements.size() == 0) {
                break;
            } else {
                webItemId++;
            }
        }
        return webItemId - 1;
    }



    private Long saveNews(Long haveMaxItemId,Long webMaxItemId) {
        //如果相等存入0条记录，直接返回
        if (haveMaxItemId == webMaxItemId) {
            return 0L;
        }

        List<News> list = new LinkedList<>();

        long updateCount = webMaxItemId - haveMaxItemId;//301-300=1
        //只更新最多UPDATE_QUANTITY条新闻信息
        if (updateCount > UPDATE_QUANTITY) {
            updateCount = UPDATE_QUANTITY;
        }

        long startItemId = webMaxItemId - updateCount + 1;
        while (startItemId <= webMaxItemId) {
            String url = URL + startItemId;
            //通过延迟5000毫秒然后再去请求可解决js异步加载获取不到数据的问题
            Document doc= null;
            try {
                doc = Jsoup.connect(url).timeout(5000).get();
            } catch (IOException e) {
                continue;
            }

            Elements title = doc.getElementsByClass("title");
            Elements info = doc.getElementsByClass("info");
            Elements content = doc.getElementsByClass("content");
            if (title.size() == 0 || info.size() == 0 || content.size() == 0) {
                CheckUtil.check("获取新闻异常", false);
            }
            News news = new News();
            String titleStr = title.get(0).text();
            String infoStr = info.get(0).text();
            String contentStr = content.get(0).text();

            news.setTitle(titleStr);
            news.setInfo(infoStr);
            news.setContent(contentStr);
            news.setItemId(startItemId);

            startItemId++;
            list.add(news);
        }

        newsRepo.saveAll(list);

        return updateCount;
    }




}
